//
//  SLPageContentView.m
//  SLPageView
//
//  Created by 王胜利 on 2018/11/12.
//  Copyright © 2018 wsl. All rights reserved.
//  跟随滑动初始化所显示的控制器，没滑过去不进行初始化
//

#define WEAK(type)   __weak typeof(type) weak##type = type;

#import "SLPageContentView.h"
#import "SLPageBaseScrollView.h"
#import "SLPageManager.h"
#import "Masonry.h"
//#import "ThemeKit.h"


@interface SLPageContentView()<UIScrollViewDelegate>

@property (nonatomic, strong) SLPageBaseScrollView *scrollView;

@end

@implementation SLPageContentView

@synthesize childViews = _childViews;
@synthesize currentIdx = _currentIdx;
@synthesize didScrollToIdx = _didScrollToIdx;
@synthesize didScrollFromToIdx = _didScrollFromToIdx;

- (instancetype)init{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setChildViews:(NSArray<UIView *> *)childViews{
    _childViews = childViews;
    [self layoutIfNeeded];
}

- (void)scrollToIndex:(NSInteger)idx animated:(BOOL)animated{
    [self layoutIfNeeded];
    if (animated) {
        WEAK(self)
        [UIView animateWithDuration:0.25 animations:^{
            weakself.scrollView.contentOffset = CGPointMake(weakself.bounds.size.width*idx, 0);
        }completion:^(BOOL finished) {
            [weakself addSubViewIfNeededWithIndex:idx];
        }];
    }else{
        self.scrollView.contentOffset = CGPointMake(self.bounds.size.width*idx, 0);
        [self addSubViewIfNeededWithIndex:idx];
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.scrollView layoutIfNeeded];
    self.scrollView.contentSize = CGSizeMake(self.bounds.size.width*self.childViews.count, 0);
}

#pragma mark - 判断是否添加子视图（避免重复添加）
- (void)addSubViewIfNeededWithIndex:(NSInteger)index{
    UIView *childView =  self.childViews[index];
    if (childView.superview) return;
    childView.frame = CGRectMake(self.scrollView.bounds.size.width * index, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
    [self.scrollView addSubview:childView];
}

#pragma mark - UIScrollViewDeleagte
// 滚动中
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    scrollView.scrollEnabled = !(scrollView.contentOffset.x < 0);
    NSInteger idx = (scrollView.contentOffset.x + self.bounds.size.width/2)/self.bounds.size.width;
    if (idx == self.currentIdx) {
        return;
    }

    NSInteger fromIndex = self.currentIdx;

    if (self.didScrollToIdx) self.didScrollToIdx(idx);
    if (self.didScrollFromToIdx) self.didScrollFromToIdx(fromIndex, idx);

    self.currentIdx = idx;
}
// 滚动结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger idx = scrollView.contentOffset.x / self.bounds.size.width;
    [self addSubViewIfNeededWithIndex:idx];
}

#pragma mark - Lazyload
- (SLPageBaseScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[SLPageBaseScrollView alloc]init];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = YES;
        [self addSubview:_scrollView];
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return _scrollView;
}



@end
